﻿using PlantsZombies.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using static PlantsZombies.Common.GDI32;

namespace PlantsZombies.View
{
    public class Class1 : Control
    {
        public Class1()
        {

        }
        protected override void OnPaint(PaintEventArgs e)
        {
            var bit = Properties.Resources.sunflowerCard;
            using (MemoryStream ms = new MemoryStream())
            {
                bit.Save(ms, bit.RawFormat);
                var buffer = ms.ToArray();

                var info = new BITMAPINFO
                {
                    bmiHeader = new BITMAPINFOHEADER
                    {
                        biSize = (uint)Marshal.SizeOf(typeof(BITMAPINFOHEADER)),
                        biWidth = bit.Width,
                        biHeight = -bit.Height, // negative so origin is in top left corner
                        biPlanes = 1,
                        biBitCount = 32, // RGBA format
                        biCompression = BI_RGB,
                    }
                };

                // 获取Graphics对象的设备上下文
                IntPtr hdc = e.Graphics.GetHdc();

                // Create a DIB from the byte[] and select it into the dc
                IntPtr ptr = CreateDIBSection(hdc, ref info, DIB_RGB_COLORS, out IntPtr imagePtr, IntPtr.Zero, 0);
                Marshal.Copy(buffer, 0, imagePtr, buffer.Length);

                // 创建兼容的设备上下文和位图
                IntPtr hMemDC = CreateCompatibleDC(hdc);

                // 使用复制的数据创建一个GDI位图对象
                IntPtr hOldBitmap = SelectObject(hMemDC, ptr);

                // 使用AlphaBlend函数进行图像混合
                AlphaBlend(hdc, 0, 0, this.Width, this.Height, hMemDC, 0, 0, bit.Width, bit.Height, blendFunction);

                // 还原设备上下文和释放资源
                SelectObject(hMemDC, hOldBitmap);
                DeleteDC(hMemDC);
                e.Graphics.ReleaseHdc(hdc);

                DeleteObject(ptr);
                bit.Dispose();
            }
        }
        private const int DIB_RGB_COLORS = 0;
        [DllImport("gdi32.dll", SetLastError = true)]
         static extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFO bmi, uint Usage, out IntPtr bits, IntPtr hSection, uint dwOffset);
        static readonly GDI32.BLENDFUNCTION blendFunction = new GDI32.BLENDFUNCTION
        {
            BlendOp = AC_SRC_OVER,
            BlendFlags = 0,
            SourceConstantAlpha = 255, // 255 表示不透明
            AlphaFormat = AC_SRC_ALPHA
        };
        [DllImport("gdi32.dll")]
        static extern int SetBitmapBits(IntPtr hbm, int dwCount, IntPtr lpBits);
        [DllImport("gdi32.dll")]
        static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
        // 定义BITMAPINFO结构
        [StructLayout(LayoutKind.Sequential)]
        private struct BITMAPINFO
        {
            public BITMAPINFOHEADER bmiHeader;
            public uint bmiColors;
        }

        // 定义BITMAPINFOHEADER结构
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        private struct BITMAPINFOHEADER
        {
            public uint biSize;
            public int biWidth;
            public int biHeight;
            public ushort biPlanes;
            public ushort biBitCount;
            public uint biCompression;
            public uint biSizeImage;
            public int biXPelsPerMeter;
            public int biYPelsPerMeter;
            public uint biClrUsed;
            public uint biClrImportant;
        }

        // 定义API常量
        private const int BI_RGB = 0;

        // 从内存创建位图
        [DllImport("gdi32.dll", EntryPoint = "CreateBitmap")]
        private static extern IntPtr CreateBitmap(int nWidth, int nHeight, uint nPlanes, uint nBitCount, IntPtr lpBits);

        // 从内存创建位图
        [DllImport("gdi32.dll", EntryPoint = "CreateBitmapFromMemory")]
        private static extern IntPtr CreateBitmapFromMemory(byte[] buffer);

        // 创建与指定设备环境兼容的内存设备环境
        [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
        private static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        // 选择对象进设备环境
        [DllImport("gdi32.dll", EntryPoint = "SelectObject")]
        private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

        // 位块传输
        [DllImport("gdi32.dll", EntryPoint = "BitBlt")]
        private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);

        // 删除设备环境对象
        [DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
        private static extern bool DeleteDC(IntPtr hdc);

        // 删除GDI对象
        [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
        private static extern bool DeleteObject(IntPtr hObject);

        // 定义常量
        private const int SRCCOPY = 0xCC0020; // 位块传输的常量
    }
}
